package com.xckj.bigdata.demo.controller;

import com.xckj.bigdata.demo.dao.UserDao;
import com.xckj.bigdata.demo.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 刘嘉民 on 2020/6/24 15:14
 */
@Controller
@RequestMapping("register")
public class RegisterController {
    @Resource
    private UserDao userDao;
    @RequestMapping("/checkname.json")
    @ResponseBody
    public Map<String,Object> checkName(String loginname){
        Map<String,Object> map = new HashMap<String,Object>();
        User user = new User();
        user.setLoginName(loginname);
       User user1= userDao.selectByParam(user);
       if (user1!=null){
           map.put("code","0");
           map.put("msg","名称已存在");
       }else{
           map.put("code","1");
           map.put("msg","恭喜你,名称可用");
       }
        return map;
    }

    @RequestMapping("/register.save")
    @ResponseBody
    public Map<String,Object> register(User user){
        Map<String,Object> map = new HashMap<String,Object>();
        User param = new User();
        param.setLoginName(user.getLoginName());
        User user1= userDao.selectByParam(param);
        if (user1!=null){
            map.put("code","0");
            map.put("msg","名称已存在");
        }else{
            userDao.insertOne(user);
            map.put("code","1");
            map.put("msg","注册成功");
        }
        return map;
    }
}
